<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author  Tran Van Thanh
 * @email   thanhtran@vietnambiz.com
 * @date    29.08.2011
 */
 
class Products_model extends MY_Model {
    
    function __construct()
    {
        parent::__construct();
        $this->_table = 'products';
    }
    
    function get_products($page = 1)
    {
    	$this->db->order_by('date_create DESC');
    	$limit = $this->config->item('products', 'limit');
    	return $this->limit($limit, ($page - 1)*$limit)->get_all();
    }
    
    function get_products_type()
    {
    	$this->db->select()
                 ->from('product_type')
                 ->order_by('name ASC');
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;
    } 
    
    function get_company()
    {
    	$this->db->select()
                 ->from('company')
                 ->order_by('order ASC');
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;
    }
    
    function get_utilities()
    {
    	$this->db->select()
                 ->from('utilities')
                 ->order_by('order ASC');
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;
    }
    
    function get_images($product_id)
    {
        if(is_numeric($product_id)) {
        	$a[] = $product_id;
        	$product_id = $a;
        }
    	return $this->db->where_in('id_product', $product_id)->get('images_products')->result();
    }
    
    function check_code_products($code_pro = null)
    {
    	$q  =   $this->db->select()->from("products")->where('code',$code_pro)->get();
        if($q->row())
        {
        	return true;
        }
        else
        {
        	return false;
		}
    }
    
    function check_code_products_edit($id_products = null, $code_pro = null)
    {
    	$where = array(
    		'code'	=> $code_pro,
    		'id !='	=> $id_products
    	);
    	$q  =   $this->db->select()->from("products")->where($where)->get();
        if($q->row())
        {
        	return true;
        }
        else
        {
        	return false;
		}
    }
    
    function get_product_type_by_id($id_product_type = null)
    {
    	$list = $this->db->select('name')
    					 ->where('id', $id_product_type)
    					 ->from('product_type')->get()->result();

    	if(count($list) > 0) {
    		$codes = array();
    		foreach($list as $row) {
    			$codes[] = $row->name;
    		}
    		
    		return implode(', ', $codes);
    	}
    	
    	return '';
    }
    
    function get_company_by_id($id_company = null)
    {
    	$list = $this->db->select('name')
    					 ->where('id', $id_company)
    					 ->from('company')->get()->result();

    	if(count($list) > 0) {
    		$codes = array();
    		foreach($list as $row) {
    			$codes[] = $row->name;
    		}
    		
    		return implode(', ', $codes);
    	}
    	
    	return '';
    }

    function delete_product($product_id)
    {
        if(is_numeric($product_id)) {
        	$a[] = $product_id;
        	$product_id = $a;
        }
    	$this->db->where_in('id_product', $product_id)->delete('images_products');
    	$this->db->where_in('id', $product_id)->delete('products');
    	$this->delete_many($product_id);
    }
}